<template>
  <div class="app-container">
    <el-card class="box-card">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="tableData"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
        >
          <el-table-column align="center" type="index" label="序号">
          </el-table-column>
          <el-table-column
            label="昵称"
            align="center"
            prop="nickName"
          ></el-table-column>
          <el-table-column label="国家" align="center" prop="country">
          </el-table-column>
          <el-table-column label="省份" align="center" prop="province">
          </el-table-column>
          <el-table-column label="区县" align="center" prop="city">
          </el-table-column>
          <el-table-column label="头像" align="center">
            <template slot-scope="scope">
              <el-image
                :src="scope.row.avatarUrl"
                style="width: 80px"
                :preview-src-list="[scope.row.avatarUrl]"
              ></el-image>
            </template>
          </el-table-column>

          <el-table-column label="性别" align="center">
            <template slot-scope="scope">
              <el-tag :type="scope.row.gender === '1' ? 'success' : ''">{{
                scope.row.gender === '1' ? '男' : '女'
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="250">
            <template slot-scope="scope">
              <el-button
                class="el-icon-view"
                type="primary"
                size="mini"
                @click="queryInfo(scope.row)"
                >查看</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="pageBox">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :page-size="pageOption.pageNum"
          :total="pageOption.total"
          :current-page="pageOption.pageSize"
          @current-change="pageJump"
          @prev-click="pageOption.pageSize--"
          @next-click="pageOption.pageSize++"
        ></el-pagination>
      </div>
      <div class="userFormData">
        <el-dialog
          :title="dialogOption.title"
          :visible.sync="dialogOption.visible"
        >
          <el-form ref="formData" :model="formData" label-width="80px">
            <el-row>
              <el-col :span="12">
                <el-form-item label="昵称：" :label-width="formLabelWidth">
                  <el-input
                    :readonly="dialogOption.required"
                    v-model="formData.nickName"
                    autocomplete="off"
                    :disabled="dialogOption.disabled"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-col :span="12">
                  <el-form-item label="性别：" :label-width="formLabelWidth">
                    <el-select
                      :readonly="dialogOption.required"
                      v-model="formData.gender"
                      placeholder="请选择性别"
                      :disabled="dialogOption.disabled"
                    >
                      <el-option label="男" value="1"></el-option>
                      <el-option label="女" value="0"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="8">
                <el-form-item label="国家：" :label-width="formLabelWidth">
                  <el-input
                    :readonly="dialogOption.required"
                    v-model="formData.country"
                    :disabled="dialogOption.disabled"
                    autocomplete="off"
                  ></el-input> </el-form-item
              ></el-col>
              <el-col :span="8">
                <el-form-item label="省份：" :label-width="formLabelWidth">
                  <el-input
                    :readonly="dialogOption.required"
                    v-model="formData.province"
                    :disabled="dialogOption.disabled"
                    autocomplete="off"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="区县：" :label-width="formLabelWidth">
                  <el-input
                    :readonly="dialogOption.required"
                    v-model="formData.city"
                    autocomplete="off"
                    :disabled="dialogOption.disabled"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="个性签名：" :label-width="formLabelWidth">
                  <el-input
                    :readonly="dialogOption.required"
                    type="textarea"
                    v-model="formData.signature"
                    maxlength="30"
                    show-word-limit
                    :disabled="dialogOption.disabled"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="12">
                <el-form-item label="微信头像：" :label-width="formLabelWidth">
                  <el-upload
                    class="avatar-uploader"
                    :action="uploadUrl"
                    :show-file-list="false"
                    :disabled="dialogOption.disabled"
                  >
                    <img
                      v-if="formData.avatarUrl"
                      :src="formData.avatarUrl"
                      class="avatar"
                    />
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogOption.visible = false">取 消</el-button>
            <el-button
              type="primary"
              v-if="dialogOption.type === 1"
              @click="dialogOption.visible = false"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getWxUserList, getWxUserInfo } from '@/api/wxuser'
export default {
  filters: {},
  data() {
    return {
      // uploadUrl: "https://www.lengnuanit.top/api/upload/upload",
      uploadUrl: 'http://127.0.0.1:7001/api/upload/upload',
      formLabelWidth: '100px',
      tableData: null,
      listLoading: true,
      pageOption: {
        pageNum: 10,
        pageSize: 1,
        total: 1,
      },
      dialogOption: {
        title: '查看微信用户',
        type: 1,
        visible: false,
        required: false,
        disabled: false,
      },
      formData: {
        avatarUrl: '',
        city: '',
        nickName: '',
        country: '',
        create_time: null,
        signature: '', //签名
        gender: '',
        language: '',
        province: '',
        update_time: null,
      },
      userId: null,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    //获取微信用户数据
    getData() {
      this.listLoading = true
      getWxUserList({
        pageNum: this.pageOption.pageNum,
        pageSize: this.pageOption.pageSize,
      }).then((res) => {
        console.log(res)
        this.tableData = res.data
        this.pageOption.total = res.total
        this.listLoading = false
      })
    },
    //查看微信用户
    queryInfo(row) {
      // console.log(row);
      this.resetForm('formData')
      this.getWxUserInfo(row.id)
      this.dialogOption.type = 1
      this.dialogOption.disabled = true
      this.dialogOption.visible = true
      this.dialogOption.required = true
      this.dialogOption.title = '查看微信用户'
    },
    getWxUserInfo(id) {
      getWxUserInfo(id).then((res) => {
        console.log(res)
        this.formData = res
      })
    },

    // 重置验证规则
    resetForm(formName) {
      // console.log(formName);
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields()
      }
    },
    // 分页跳转
    pageJump(page) {
      //console.log(page);
      this.pageOption.pageSize = page
      this.getData()
    },
  },
}
</script>
<style lang="scss" scope>
.addUser {
  padding: 0 0 20px 0;
}
.pageBox {
  text-align: right;
  padding: 10px 0 20px 0;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
